package com.situ.springboot.service.impl;

import com.situ.springboot.mapper.RecordMapper;
import com.situ.springboot.pojo.vo.CountCustomidVo;
import com.situ.springboot.pojo.vo.CountServiceidVo;
import com.situ.springboot.pojo.vo.RecordVO;
import com.situ.springboot.service.RecordService;
import com.situ.springboot.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RecordServiceImpl implements RecordService {

    @Autowired
    private RecordMapper recordMapper;

    @Override
    public PageInfo selectByPage(Integer pageNo, Integer pageSize) {
        // limit
        int offset = (pageNo - 1) * pageSize;
        //查找当前也数据
        List<RecordVO> list = recordMapper.selectByPage(offset, pageSize);
        //查找总的数量，目的是为了算总的页数totalPage
        int totalCount = recordMapper.selectTotalCount();
        int totalPage = (int)Math.ceil((double) totalCount / pageSize);

        return new PageInfo<>(list, totalPage, pageNo, pageSize);
    }

    @Override
    public void update(RecordVO recordVO) {
        recordMapper.update(recordVO);
    }

    @Override
    public RecordVO selectById(Integer id) {
        return recordMapper.selectById(id);
    }

    @Override
    public List<RecordVO> selectByCustomId(Integer id) {
        return recordMapper.selectByCustomId(id);
    }

    @Override
    public void add(RecordVO recordvo) {
        recordMapper.add(recordvo);
    }

    @Override
    public Integer selectByCustomIdAndServiceId(Integer customId, Integer serviceId) {
        List<RecordVO> recordVOS = recordMapper.selectByCustomIdAndServiceId(customId, serviceId);
        if(recordVOS.isEmpty()) return 0;
        return 1;
    }

    //RecordServiceImpl.java
    @Override
    public List<CountCustomidVo> selectByCustomIdCount() {
        return  recordMapper.selectByCustomIdCount();
    }

    @Override
    public List<CountServiceidVo> selectByServiceIdCount() {
        return recordMapper.selectByServiceIdCount();
    }

}
